package cn.yunhe.dao.impl;

import cn.yunhe.dao.IAdminDao;
import cn.yunhe.entity.Admin;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("adminDao")
public class AdminDaoImpl extends HibernateTemplate implements IAdminDao {

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public Admin login(Admin admin) {
            List<Admin> adminList = find("from Admin a where a.username = ? and a.password = ?", admin.getUsername(), admin.getPassword());
            if (adminList != null && adminList.size() > 0) {
                return adminList.get(0);
            } else {
                return null;
            }

       /* Criteria criteria = super.getSessionFactory().openSession().createCriteria(Admin.class);
        List<Admin> adminList = criteria.add(Restrictions.eq("username",admin.getUsername())).add(Restrictions.eq("password",admin.getPassword())).list();
        if (adminList != null && adminList.size()>0){
            return adminList.get(0);
        }else {
            return null;
        }*/
    }

    @Override
    public void register(Admin admin) {
            super.save(admin);
    }


    @Override
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
